import axios from 'axios';
import store from '../store';
import config from './config';
import router from '@/router'
import { MessageBox, Message } from 'element-ui'
import { getStorage, setStorage, removeStorage } from '@/utils/storage'
import { JSEncrypt } from 'encryptlong' //引用
const interfaceApi = axios.create({

	timeout: config.api.overtime,
	baseURL: config.api.url
});
//请求拦截
interfaceApi.interceptors.request.use((options) => {
	options.url = config.api.url + options.url
	if (store.state.user.token) {
		options.headers.Authorization = 'Bearer ' + store.state.user.token;
	}
	return options;
});

//响应拦截
interfaceApi.interceptors.response.use((response) => {
	if (response.status != 200) {
		return Promise.reject("系统异常2")
	}
	if (!response) {
		Message.error("网络错误")
		return Promise.reject("网络错误")
	}
	if (!response) {
		Message.error("系统异常3")
		return Promise.reject("系统异常3")

	}
	let current_path = router.history.current

	let decrypt = new JSEncrypt();

	decrypt.setPrivateKey(config.cert.privateKey);

	var decryptMsg = decrypt.decryptLong(response.data, 'utf-8');
	response = JSON.parse(decryptMsg);
	console.log('response', response)
	if (response[config.api.codeField] == config.api.overdueCode) {
		store.dispatch('system/showLogin')
		store.dispatch('user/signOut')
		if (current_path.meta && current_path.meta.redirect) {
			router.push({ path: '/' })
		}
		return Promise.reject(response)
	}
	if (response[config.api.codeField] == 305) {
		console.log('未实名')
		store.dispatch('system/showAuth')
		Message({
			message: response.message,
			type: 'error',
			customClass: 'super-message',
		})
		return Promise.reject(response.message)
	}
	if (response[config.api.codeField] == 302) {
		return Promise.reject(response)
	}
	if (response[config.api.codeField] == 301) {
		Message({
			message: "请先前往手机APP设置支付密码",
			type: 'error',
			customClass: 'super-message',
		})
		return Promise.reject(response)
		
	}
	if (response[config.api.codeField] == 303) {
		store.state.system.live_pay_visible = true
		return Promise.reject(response)
	}

	if (response[config.api.codeField] != config.api.successCode) {
		console.log(response, 234234)
		if (response.data && !response.data.no_err) {
			Message({
				message: response[config.api.messgaeField] || '系统异常4',
				type: 'error',
				customClass: 'super-message',
			})
		}
		return Promise.reject(response || '系统异常4')
	}
	return response[config.api.resultField] || response;


}, error => {
	return Promise.reject("系统异常5")
});

const request = async (url, data, method, isLoading = false, headers) => {
	if (isLoading) {
		store.commit('data/loading', true);
	}
	return new Promise(async (resolve, reject) => {

		interfaceApi.request({
			method,
			url,
			data: method != 'get' && data,
			params: method == "get" && data,
		}).then(res => resolve(res))
			.catch(error => {
				reject(error);
			}).finally(() => {
				if (isLoading) {
					store.commit('data/loading', false);
				}
			})

	})

}


export const get = (url, data = {}, loading = false) => {
	return request(url, data, 'get', loading);
}

export const post = (url, data = {}, loading = false) => {
	return request(url, data, 'post', loading);
}

export const put = (url, data = {}, loading = false) => {
	return request(url, data, 'put', loading);
}

export const del = (url, data = {}, loading = false) => {
	return request(url, data, 'delete', loading);
}

export const upload = (url, data = {}, loading = false) => {
	const formData = new FormData();
	for (let key in data) {
		formData.append(key, data[key]);
	}
	return request(url, formData, 'post', loading, { "Content-Type": "multipart/form-data" });
}


export default {
	get, post, put, del, upload
}